//
//  SingleSecondDetailsViewController.m
//  项目A --半糖
//
//  Created by romana on 16/5/26.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "SingleSecondDetailsViewController.h"
#import "AFNetworking.h"
#import "SingleDetailsVCModel.h"
#import "SingleDetailsVCSecondModel.h"
#import "SingleDetailsSmallCollectionViewCell.h"
#import "SingleDetailsBigCollectionModel.h"
#import "SingleDetailsBigCollectionViewCell.h"
#import "SingleThirdDetailsViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SingleSecondDetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,strong)UICollectionView *mySmallCollectionView;
@property(nonatomic,strong)UICollectionView *myBigCollectionView;

@property(nonatomic,strong)NSMutableArray *mySmallArray;
@property(nonatomic,strong)NSMutableArray *myBigArray;
//@property(nonatomic,strong)SingleDetailsSmallCollectionViewCell *myitem;

//用来接收点击小cell传过去的id
@property(nonatomic,copy)NSString *cellID;



@end

@implementation SingleSecondDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = self.sinSVCModel.name;
    self.view.backgroundColor = [UIColor yellowColor];
 
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:244.0 / 255.0 green:244.0 / 255.0 blue:244.0 / 255.0 alpha:1.0];
    //self.navigationController.navigationBar.barTintColor = [UIColor redColor];
   // NSLog(@"%@",self.sinSVCModel.modelID);
    
    self.mySmallArray = [NSMutableArray array];
    self.myBigArray = [NSMutableArray array];
    
    [self getSmallData];
    [self isGetData];
  // [self getBigData];
  
    
//小collectionview
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 40);
    flowLayout.minimumInteritemSpacing = 10;
  //      flowLayout.row = 1;// 行数
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    
     self.mySmallCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , 60) collectionViewLayout:flowLayout];
    
    self.mySmallCollectionView.showsHorizontalScrollIndicator = NO;
    self.mySmallCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mySmallCollectionView];
    
    self.mySmallCollectionView.delegate = self;
    self.mySmallCollectionView.dataSource = self;


    [self.mySmallCollectionView registerClass:[SingleDetailsSmallCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
   
    
    //大collectionview
    UICollectionViewFlowLayout *flowLayout1 = [UICollectionViewFlowLayout new];
    flowLayout1.itemSize = CGSizeMake(190, 250);
    flowLayout1.minimumInteritemSpacing = 10;
    flowLayout1.minimumLineSpacing = 10;
    flowLayout1.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout1.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.myBigCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 49 - 64 - 60) collectionViewLayout:flowLayout1];
    
    self.myBigCollectionView.showsHorizontalScrollIndicator = NO;
    self.myBigCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myBigCollectionView];
    
    self.myBigCollectionView.delegate = self;
    self.myBigCollectionView.dataSource = self;
    
    [self.myBigCollectionView registerClass:[SingleDetailsBigCollectionViewCell class] forCellWithReuseIdentifier:@"bigReuse"];
    
    
    
    // Do any additional setup after loading the view.
}



-(void)isGetData{

    NSString *oneUrlString = @"iPhone7%2C1";
    NSString *urlString = [NSString stringWithFormat:@"http://open3.bantangapp.com/search/product/listBySubclass?app_id=com.jzyd.BanTang&app_installtime=1434705307&app_versions=5.8&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&id=%@&oauth_token=8a11f2b3cd8fd625a8d8e9effb21dd0f&os_versions=9.3.2&screensize=1242&sort_type=0&track_device_info=%@&track_deviceid=C2D3B460-4E0E-4639-A030-D274CFC43D50&track_user_id=125107&v=13",self.sinSVCModel.modelID,oneUrlString];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *bigDic = responseObject;
        NSMutableArray *dataArray = [bigDic objectForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            SingleDetailsBigCollectionModel *model = [SingleDetailsBigCollectionModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.myBigArray addObject:model];
        }
        
        [self.myBigCollectionView reloadData];
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    


}

-(void)getBigData{
    
    NSLog(@"CELLID=====%@",self.cellID);
    

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
       [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    NSDictionary *dic = @{@"app_id":@"com.jzyd.BanTang",@"app_installtime":@"1434705307",@"app_versions":@"5.8",@"channel_name":@"appStore",@"client_id":@"bt_app_ios",@"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5",@"id":self.cellID,@"oauth_token":@"8a11f2b3cd8fd625a8d8e9effb21dd0f",@"os_versions":@"9.3.2",@"screensize":@"1242",@"sort_type":@"0",@"track_device_info":@"iPhone7%2C1",@"track_deviceid":@"C2D3B460-4E0E-4639-A030-D274CFC43D50",@"track_user_id":@"125107",@"v":@"13"};
    [manager POST:@"http://open3.bantangapp.com/search/product/listByClass" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *bigDic = responseObject;
        NSMutableArray *dataArray = [bigDic objectForKey:@"data"];
        [self.myBigArray removeAllObjects];
        for (NSDictionary *dic in dataArray) {
            SingleDetailsBigCollectionModel *model = [SingleDetailsBigCollectionModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.myBigArray addObject:model];
        }
        
        [self.myBigCollectionView reloadData];
        

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];


}

-(void)getSmallData{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    
    NSDictionary *dic = @{@"app_id":@"com.jzyd.BanTang",@"app_installtime":@"1434705307",@"app_versions":@"5.8",@"channel_name":@"appStore",@"client_id":@"bt_app_ios",@"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5",@"id":self.sinSVCModel.modelID,@"oauth_token":@"8a11f2b3cd8fd625a8d8e9effb21dd0f",@"os_versions":@"9.3.2",@"screensize":@"1242",@"track_device_info":@"iPhone7%2C1",@"track_deviceid":@"C2D3B460-4E0E-4639-A030-D274CFC43D50",@"track_user_id":@"125107",@"v":@"13"};
    
    [manager POST:@"http://open3.bantangapp.com/category/subclassInfo" parameters:dic progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *bigDic = responseObject;
        
        NSMutableDictionary *dataDic = [bigDic objectForKey:@"data"];
        SingleDetailsVCModel *sinDVCModel = [[SingleDetailsVCModel alloc] init];
        [sinDVCModel setValuesForKeysWithDictionary:dataDic];
        for (SingleDetailsVCSecondModel *model in sinDVCModel.class_list) {
            [self.mySmallArray addObject:model];
        }
        
        
        
        [self.mySmallCollectionView reloadData];
        
        //默认点击第一个item
        [self.mySmallCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    
        
        //[self.myBigCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
        
        [self.myBigCollectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];



}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.mySmallCollectionView) {
        return self.mySmallArray.count;

    }else{
    
        return self.myBigArray.count;
    }
   


}


//-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"1111");
//    self.myitem.backgroundColor = [UIColor yellowColor];
//}
//
//-(void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
//  //  self.myitem.backgroundColor = [UIColor whiteColor];
//    NSLog(@"2222");
//}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.mySmallCollectionView) {
        SingleDetailsSmallCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        item.backgroundColor = [UIColor whiteColor];
        
        //点击cell 改变背景颜色
        UIView *backgroundView = [UIView new];
        backgroundView.backgroundColor = [UIColor lightGrayColor];
        item.selectedBackgroundView = backgroundView;
       
        
        
        item.SDVCModel = [self.mySmallArray objectAtIndex:indexPath.row];
      //  self.myitem = item;
        return item;
    }
    else{
    
        SingleDetailsBigCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"bigReuse" forIndexPath:indexPath];
        
        item.model = [self.myBigArray objectAtIndex:indexPath.row];
      //  item.backgroundColor = [UIColor magentaColor];
        return  item;
    
    
    }
   
}

//点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (collectionView == self.mySmallCollectionView) {
        SingleDetailsVCSecondModel *model = [self.mySmallArray objectAtIndex:indexPath.row];
        self.cellID = model.modelID;
        
       //  NSLog(@"cellID=====%@",self.cellID);
        [self getBigData];
     //   [self.myBigCollectionView reloadData];

    } else {
     
        SingleDetailsBigCollectionModel *model = [self.myBigArray objectAtIndex:indexPath.row];
        SingleThirdDetailsViewController *STDVC = [[SingleThirdDetailsViewController alloc] init];
        STDVC.SDBigModel = model;
        [self.navigationController pushViewController:STDVC animated:YES];
        
    }



}

@end
